package com.ls.service.impl;

import com.ls.dao.DepartmentDao;
import com.ls.entity.Department;
import com.ls.entity.JsonResult;
import com.ls.entity.Page;
import com.ls.service.IDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;



@Service("departService")
public class DepartServiceImpl implements IDepartService {
	@Autowired
	DepartmentDao dao;

	/**
	 * 查所有部门
	 */
	public JsonResult<List<Department>> selectAll(int pageId, int num) {
		JsonResult<List<Department>> result = new JsonResult<List<Department>>();
		Page page = new Page();
		int jilu = dao.findPageNum().size();
		List<Department> find = dao.findAllDepartments((pageId - 1) * num, num);
		if (find != null) {
			page.setPageId(pageId);
			page.setPageNum(jilu % num == 0 ? jilu / num : jilu / num + 1);
			result.setPage(page);
			result.setStatus(0);
			result.setData(find);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询部门失败");
		;
		return result;
	}

	public JsonResult<List<Department>> selectAllDepartment() {
		JsonResult<List<Department>> result = new JsonResult<List<Department>>();
		List<Department> find = dao.findPageNum();

		if (find != null) {
			result.setStatus(0);
			result.setData(find);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询部门失败");
		;
		return result;
	}

	/**
	 * 添加部门
	 */
	public JsonResult<Object> insertDept(String dept_name, Date create_time) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.insertDept(dept_name, create_time);
		if (i == 0) {
			result.setStatus(1);
			result.setMsg("添加部门失败");
			return result;
		}
		result.setStatus(0);
		result.setData("true");
		return result;
	}

	/**
	 * 删部门
	 */
	public JsonResult<Object> deleteByIdDept(String[] ids) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.deleteByIdDept(ids);
		// 失败
		if (i == 0) {
			result.setStatus(1);
			result.setMsg("删除部门失败");
			return result;
		}
		result.setStatus(0);
		result.setData(i);
		return result;
	}

	/**
	 * 根据部门名称查询
	 */
	public JsonResult<List<Department>> findName(String name, int pageId, int num) {
		JsonResult<List<Department>> result = new JsonResult<List<Department>>();
		Page page = new Page();
		int jilu = dao.findName(name).size();
		List<Department> list = dao.findNameFenye(name, (pageId - 1) * num, num);
		if (list != null) {
			page.setPageId(pageId);
			page.setPageNum(jilu % num == 0 ? jilu / num : jilu / num + 1);
			result.setStatus(0);
			result.setData(list);
			result.setPage(page);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询失败");
		return result;
	}

	public JsonResult<Department> showDp(int dept_id) {
		JsonResult<Department> result = new JsonResult<Department>();
		 Department department = dao.showDp(dept_id);
		if (department!=null) {
			result.setStatus(0);
			result.setData(department);
			return result;
		}
		result.setStatus(1);
		result.setMsg("show客户失败了");
		return result;
	}

	public JsonResult<Object> updateDp(Department department) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.updateDp(department);
		if (i==1) {
			result.setStatus(0);
			result.setData(i);
			return result;
		}
		result.setStatus(1);
		result.setMsg("修改部门失败了");
		return result;
	}


}
